In object-oriented programming (OOP), object lifetime is the period of time between an object's creation and its destruction. In some programming contexts, object lifetime coincides with the lifetime of a variable that represents the object. In other contexts where the object is accessed by reference object lifetime is not determined by the lifetime of a variable. For example, destruction of the variable may only destroy the reference, not the referenced object.
The syntax for creation and destruction varies by programming context. In many contexts, including C++, C# and Java, an object is created via special syntax like
An alternative and deterministic approach to automatic destruction is where the object is destroyed when code decrements the object's reference count to zero.
With an object pool, where objects may be created ahead of time and reused, the apparent creation and destruction of an object may not correspond to actual. The pool provides reinitialization for creation and finalization for destruction. Both creation and destruction may be non-deterministic.
Objects with static memory allocation have a lifetime that coincides with the run of a program, but the order of creation and destruction of the various static objects is generally non-deterministic.
Life cycle generally includes memory management and operations after allocation and before deallocation. Object creation generally consists of memory allocation and initialization where initialization includes assigning values to fields and running initialization code. Object destruction generally consists of finalization (a.k.a. cleanup) and memory deallocation (a.k.a. free). These steps generally proceed in order as: allocate, initialize, finalize, deallocate.
Based on programming context, these steps may be partially or fully automated, and some of the steps can be customized.
The frequency of customization tends to vary by step and programming context. Initialization is the most commonly customized step. Finalization is common in languages with deterministic destruction, notably C++, but rare in garbage-collected languages. Allocation is rarely customized, and deallocation generally cannot be customized.
In some cases, object destruction consists solely of deallocating memory, particularly with garbage-collection, or if the object is a plain old data structure. In other cases, cleanup is performed prior to deallocation, particularly destroying member objects (in manual memory management), or deleting references from the object to other objects to decrement reference counts (in reference counting). This may be automatic, or a special destruction method may be called on the object.
In class-based languages with deterministic object lifetime, notably C++, a destructor is called when an instance is deleted, before the memory is deallocated. In C++, destructors differ from constructors in various ways. They cannot be overloaded, must have no arguments, need not maintain , and can cause program termination if they throw exceptions.
With garbage collection, objects may be destroyed when they can no longer be accessed by the program. The garbage-collector calls a finalizer before memory deallocation.
Destroying an object will cause any references to the object to become invalid. With manual memory management, any existing reference becomes a dangling reference. With garbage collection, objects are only destroyed when there are no references to them.
If creation or destruction fail, error reporting (for example, raising an exception) can be complicated since the object or related objects may be in an inconsistent state.
For a static variable, with lifespan coinciding with the program run, a creation or destruction error is problematic since program execution is before or after normal execution.
Notably, a constructor is a class method as there is no object (instance) available until the object is created, but destructors, initializers, and finalizers are instance methods.
Further, constructors and initializers often can accept arguments, while destructors and finalizers generally do not as they are often implicitly callable.
// ...
};
When declared in an automatic context, the object is destroyed at the close of the block it is declared.
Foo foo;
// foo is destroyed here
}
When created dynamically, it lives until it is explicitly destroyed.
Foo* foo = new Foo();
delete foo;
}
When using , its destruction semantics are controlled based on scope or shared references.
// ...
}
After an instance is created (i.e.
fn example<'a>(file: &'a File) {
// ...
}
|
|